package viper.main;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import viper.GUI.LoggedInPanel;
import viper.GUI.LoggedOutPanel;
import viper.GUI.ViperGUI;

public class StateManager {
	/**
	 * Singleton structure
	 */
	public final static StateManager INSTANCE = new StateManager();
	private StateManager(){
		
	}
	//--------------------------------------------------------------
	
	/**
	 * State
	 */
	private enum State {
		LOGGED_OUT, NOT_IN_CALL, IN_CALL 
	}
	private State state = State.LOGGED_OUT;
	
	private void setState(State state) {
		this.state = state;
	}

	public State getState() {
		return state;
	}
	//----------------------------------------------------------------
	
	private String username;
	
	public String getIP() {
		String ip = null;
		try {
			InetAddress address = InetAddress.getLocalHost();
			ip = address.getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return ip;
	}
	
	public void login(String name){
		setUsername(name);
		MySQLFunctions.login(getIP(), name);
		setState(State.NOT_IN_CALL);
		ViperGUI.INSTANCE.changePanel(new LoggedInPanel());
	}
	
	public void logout(){
		MySQLFunctions.logout(getIP());
		setState(State.LOGGED_OUT);
		ViperGUI.INSTANCE.changePanel(new LoggedOutPanel());
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}
	
	public ArrayList<Object[]> getPeers() {
		return MySQLFunctions.getCurrentUsers();
	}
}
